package com.wisdomstar.wordfilter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.wordfilter.domain.Word;
import com.wisdomstar.wordfilter.mapper.WordMapper;
import com.wisdomstar.wordfilter.service.WordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/10
 */
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, Word> implements WordService {

    @Resource
    private WordMapper wordMapper;

    @Override
    public int removeWord(Long wordId) {
        Word word = new Word();
        word.setWordId(wordId);
        return wordMapper.removeWord(word);
    }

    @Override
    public int insertWord(Word word) {
        String wordContent = word.getWordContent();
        int count = wordMapper.checkWord(word.getWordId(), wordContent);
        if (count != 0){
            throw new BaseException("过滤词已存在");
        }
        return wordMapper.insertWord(word);
    }

    @Override
    public PageInfo<Word> listWord(Word word, Integer pageNum, Integer pageSize) {
        PageMethod.startPage(pageNum, pageSize);
        List<Word> wordList = wordMapper.listWord(word);
        return new PageInfo<>(wordList);
    }

    @Override
    public int updateWord(Word word) {
        String wordContent = word.getWordContent();
        int count = wordMapper.checkWord(word.getWordId(), wordContent);
        if (count != 0){
            throw new BaseException("过滤词已存在");
        }
        return wordMapper.updateWord(word);
    }

    @Override
    public List<String> findWordContent(String wordType) {
        return wordMapper.findWordContent(wordType);
    }
}
